﻿package desk.game.eatbean;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class MyCanvas extends Canvas implements Runnable{
	private int width,height;
	private int p;
	Thread t;
	MySprite mysprite;
	Beans bean;
	Enemy enemy;
	boolean flag=true;
	public MyCanvas()
	{
		width=getWidth();
		height=getHeight();
		mysprite=new MySprite(10,10,width,height);	
		enemy=new Enemy(100,100,width,height);
		t=new Thread(this);
		bean=new Beans(width,height);
		t.start();
	}
	
	public void rest()
	{
		flag=false;
		enemy.setSpeed();
		new Beans();
	}
	
	protected void keyPressed(int keyCode)
	{
		int gameAction=getGameAction(keyCode);
		switch(gameAction)
		{
		case UP:p=2;break;
		case DOWN:p=8;break;
		case LEFT:p=4;break;
		case RIGHT:p=6;break;
		default: p=0;break;
		}
	}

	protected void paint(Graphics g) {
			g.setColor(0,0,0);
			g.fillRect(0,0,width,height);		
			bean.paint(g);
			mysprite.paint(g);
			enemy.paint(g);
			bean.drawScore(g);
			enemy.drawFail(g);
			bean.drawWin(g);
		}
	
	public void run() {
		while(flag&&bean.isWin())
		{
			repaint();
			try {
				Thread.sleep(100);
			}catch (InterruptedException e) {
				e.printStackTrace();
			}
			mysprite.move(p);
			enemy.run(mysprite.getX(), mysprite.getY());
			if(enemy.getP()==0)
			{
				repaint();
				break;
			}
		}
	}

}
